package com.ruoyi.student.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.student.domain.DTO.ReCourseClassDTO;
import com.ruoyi.student.domain.ReCourseClass;
import com.ruoyi.student.domain.vo.TClassVo;
import com.ruoyi.student.service.IReCourseClassService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.student.domain.TClass;
import com.ruoyi.student.service.ITClassService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 班级Controller
 * 
 * @author ruoyi
 * @date 2023-08-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/student/class")
public class TClassController extends BaseController
{
    @Autowired
    private ITClassService tClassService;
    @Autowired
    private IReCourseClassService iReCourseClassService;

    /**
     * 查询班级列表
     */
    @PreAuthorize("@ss.hasPermi('student:class:list')")
    @GetMapping("/list")
    public TableDataInfo list(TClass tClass)
    {
        startPage();
//        List<TClass> list = tClassService.selectTClassList(tClass);
        List<TClassVo> list = tClassService.selectTClassVoList(tClass);
        return getDataTable(list);
    }

    /**
     * 导出班级列表
     */
    @PreAuthorize("@ss.hasPermi('student:class:export')")
    @Log(title = "班级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TClass tClass)
    {
        List<TClass> list = tClassService.selectTClassList(tClass);
        ExcelUtil<TClass> util = new ExcelUtil<TClass>(TClass.class);
        util.exportExcel(response, list, "班级数据");
    }

    /**
     * 获取班级详细信息
     */
    @PreAuthorize("@ss.hasPermi('student:class:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tClassService.selectTClassVoById(id));
    }
//    public AjaxResult getInfo(@PathVariable("id") Long id)
//    {
//        return success(tClassService.selectTClassById(id));
//    }

    /**
     * 新增班级
     */
    @PreAuthorize("@ss.hasPermi('student:class:add')")
    @Log(title = "班级", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TClass tClass)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        tClass.setCreatorId(user.getUserId());
        return toAjax(tClassService.insertTClass(tClass));
    }

    /**
     * 修改班级
     */
    @PreAuthorize("@ss.hasPermi('student:class:edit')")
    @Log(title = "班级", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TClass tClass)
    {
        return toAjax(tClassService.updateTClass(tClass));
    }

    /**
     * 删除班级
     */
    @PreAuthorize("@ss.hasPermi('student:class:remove')")
    @Log(title = "班级", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tClassService.deleteTClassByIds(ids));
    }

    /**
     * 移除课程
     */
    @PreAuthorize("@ss.hasPermi('student:class:removecourse')")
    @Log(title = "移除课程", businessType = BusinessType.DELETE)
    @PostMapping("/delReClassCourse")
    public AjaxResult removeCourse(@RequestBody ReCourseClassDTO reCourseClassDTO)
    {
        Long[] courseIds = reCourseClassDTO.getCourseIds();
        try {
            for(int i=0;i<courseIds.length;i++) {
                ReCourseClass reCourseClass = new ReCourseClass();
                reCourseClass.setValidFlag(0);
                reCourseClass.setCourseId(courseIds[i]);
                reCourseClass.setClassId(reCourseClassDTO.getClassId());
                iReCourseClassService.removeReCourseClassBycourseIdAndClassId(reCourseClass);
            }
            return toAjax(1);
        }catch (Exception e){
            return toAjax(0);
        }
    }

    /**
     * 添加课程
     */
    @PreAuthorize("@ss.hasPermi('student:class:submitAddCourse')")
    @Log(title = "添加课程", businessType = BusinessType.INSERT)
    @PostMapping("/addReClassCourse")
    public AjaxResult submitAddCourse(@RequestBody ReCourseClassDTO reCourseClassDTO)
    {
        Long[] courseIds = reCourseClassDTO.getCourseIds();
        for(int i=0;i<courseIds.length;i++) {
            ReCourseClass reCourseClass = new ReCourseClass();
            reCourseClass.setClassId(reCourseClassDTO.getClassId());
            reCourseClass.setCourseId(courseIds[i]);
            reCourseClass.setValidFlag(1);
            reCourseClass.setCreateTime(new Date());
            iReCourseClassService.insertReCourseClass(reCourseClass);
        }
        return toAjax(1);
    }
}
